﻿using Microsoft.Maui.Controls;
using Microsoft.Maui.Essentials;
using Newtonsoft.Json;
using RSModels.Data;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Text;

namespace RSDriverMobile
{
    public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            InitializeComponent();
        }

        public List<RSTask> Tasks { get; set; }

        private void OnLoginClicked(object sender, EventArgs e)
        {
            if (int.TryParse(driverIDLabel.Text, out int driverID) && int.TryParse(carIDEditor.Text, out int carID))
            {
                HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create("http://47.103.144.211:5000/api/Tasks/GetCurrentTasks");
                httpWebRequest.Method = "POST";
                MultipartFormDataContent content = new();
                content.Headers.Add("ContentType", $"multipart/form-data");
                content.Add(new StringContent("1"), "id");
                content.Add(new StringContent("1"), "title");
                content.Add(new StringContent("1"), "content");
                content.Add(new StringContent(DateTime.Now.ToString()), "eventTime");
                content.Add(new StringContent(carID.ToString()), "carID");
                content.Add(new StringContent(driverID.ToString()), "driverID");
                content.Add(new StringContent("1"), "currentLoad");
                content.Add(new StringContent("1"), "lng");
                content.Add(new StringContent("1"), "lat");
                try
                {
                    HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                    StreamReader streamReader = new(httpWebResponse.GetResponseStream(), Encoding.UTF8);
                    string jsonText = streamReader.ReadToEnd();
                    streamReader.Close();
                    httpWebResponse.Close();
                    Tasks = JsonConvert.DeserializeObject<List<RSTask>>(jsonText);
                    if (Tasks != null)
                    {
                        MainPageGrid.IsVisible = false;
                        TaskPageGrid.IsVisible = true;
                        NextTask();
                    }
                    else
                        DisplayAlert("错误", "解析数据失败", "好的");
                }
                catch
                {
                    DisplayAlert("错误", "从服务器获取数据失败", "好的");
                }
                //Navigation.PushAsync(new TaskPage());
                //DisplayAlert("当前任务", jsonText, "OK");
                //MainPageGrid.IsVisible = false;
                //TaskPageGrid.IsVisible = true;
            }
            else
                DisplayAlert("提示", "参数格式错误", "好的");
        }

        private void OnNextClicked(object sender, EventArgs e)
        {
            NextTask();
        }

        private void NextTask()
        {
            if (Tasks != null && Tasks.Count > 0)
            {
                taskLabel.Text = Tasks[0].Content;
                Tasks.RemoveAt(0);
            }
            else
                taskLabel.Text = "暂无任务";
        }
    }
}
